/*
 * @Author: DUST
 * @Date: 2023-05-16 14:40:15
 * @LastEditors: DUST
 * @LastEditTime: 2024-11-11 15:42:47
 * @FilePath: \business-card-system\admin\src\views\admin\businessCardUser\index.ts
 * @Description: 
 * undefined
 */
import BaseTable from "@/class/table";
import { Ref, reactive, ref } from "vue";
import { businessCardUserPage, addBusinessCardUser, editBusinessCardUser, deleteBusinessCardUser, companyAddListAPi } from "@/api";
import { Ajax, IButton } from "@/interface";
import timeFormat from "@/utils/time";
import { modalType } from "@/interface/enum";
import { message } from "ant-design-vue";
import user from "@/store/user";

export interface tableItem {
  index: number;
  name: string;
  avatar: string;
  wxNumber: string;
  mobile: string;
  email: string;
  job: string;
  id: number;
}
interface conformObj {
  name: string;
  avatar: string;
  wxNumber: string;
  mobile: string;
  email: string;
  job: string;
}

class NoteManage extends BaseTable<tableItem> {
  /**
   * @description: 表格columns
   */
  public columns = reactive([
    {
      title: "序号",
      customRender: ({ index }: { index: number }) => index + 1,
      fixed: "left",
      width: 60
    },
    {
      title: "头像",
      dataIndex: "avatar",
      action: "previewAvatar",
      width: 70,
    },
    {
      title: "姓名",
      dataIndex: "name",
      width: 120,
      ellipsis: true
    },
    {
      title: "手机号",
      dataIndex: "mobile",
    },
    {
      title: "邮箱",
      dataIndex: "email",
      ellipsis: true
    },
    {
      title: "微信号",
      dataIndex: "wxNumber",
      ellipsis: true
    },
    {
      title: "职位",
      dataIndex: "job",
      ellipsis: true
    },
    {
      title: "创建时间",
      dataIndex: "createTime",
      customRender: ({ text }: { text: number }) : string => timeFormat(text, "YYYY-MM-DD HH:mm:ss") 
    },
    {
      title: "修改时间",
      dataIndex: "upTime",
      customRender: ({ text }: { text: number }) : string => timeFormat(text, "YYYY-MM-DD HH:mm:ss") 
    },
    {
      title: "名片",
      action: "columnCell",
      width: 80
    },
    {
      title: "操作",
      action: "operation",
      width: 110
    }
  ]);

  /**
   * @description: 新增、编辑景区弹窗控制器
   */
  private _visible = ref<boolean>(false);
  public get visible(): Ref<boolean> {
    return this._visible;
  }
  /**
   * @description: loading
   */
  private _loading = ref<boolean>(false);
  public get loading(): Ref<boolean> {
    return this._loading;
  }
  /**
   * @description: editData
   */
  private _editData = ref<tableItem | undefined>();
  public get editData(): Ref<tableItem | undefined> {
    return this._editData;
  }
  /**
   * @description: 新增、编辑景区弹窗控制器
   */
  private _type = ref<modalType>(modalType.ADD);
  public get type(): Ref<modalType> {
    return this._type;
  }
  /**
   * @description: 显示新增弹窗
   */
  public showAdd = () => {
    this._visible.value = true;
    this._type.value = modalType.ADD;
  };
  /**
   * @description: 显示编辑弹窗
   */
  public showEdit = (record: tableItem) => {
    this._visible.value = true;
    this._type.value = modalType.EDIT;
    this._editData.value = record;
  };

  /**
   * @description: 按钮列表
   */
  private _buttonList = ref<Array<IButton>>([
    {
      name: "新增人员",
      type: "primary",
      func: this.showAdd
    }
  ]);
  public get buttonList(): Ref<Array<IButton>> {
    return this._buttonList;
  }

  /**
   * @description: 地址列表
   */
  private _addrList = ref<Array<string>>([]);
  public get addrList(): Ref<Array<string>> {
    return this._addrList;
  }

  /**
   * @description: 公司列表
   */
  private _companyList = ref<Array<string>>([]);
  public get companyList(): Ref<Array<string>> {
    return this._companyList;
  }

  /**
   * @description: 子类构造函数
   */
  constructor() {
    super({ AJAXFunc: businessCardUserPage });
    this.getCompanyAddListAPi();
  }
  /**
   * @description: 获取随机十六进制颜色
   */
  public color16 = () => {
    //十六进制颜色随机
    const r = Math.floor(Math.random() * 200);
    const g = Math.floor(Math.random() * 200);
    const b = Math.floor(Math.random() * 200);
    const color = `#${r.toString(16).padStart(2, "0")}${g
      .toString(16)
      .padStart(2, "0")}${b.toString(16).padStart(2, "0")}`;
    // console.log(color);
    return color;
  };
  /**
   * @description: 新增记录
   * @param {conformObj} obj
   */  
  public addFunc = (obj: conformObj) => {
    addBusinessCardUser(obj).then(() => {
      message.success("新增成功");
      this._visible.value = false;
      this.getData();
    }).finally(() => {
      this._loading.value = false;
    })
  }
  /**
   * @description: 编辑记录
   * @param {conformObj} obj
   */  
  public editFunc = (obj: conformObj) => {
    editBusinessCardUser(obj).then(() => {
      message.success("编辑成功");
      this._visible.value = false;
      this.getData();
    }).finally(() => {
      this._loading.value = false;
    })
  }

  /**
   * @description: 确认提交
   * @param {object} param1
   */
  public doOk = ({type, obj}: { type: modalType; obj: conformObj }) => {
    if(type == modalType.ADD) this.addFunc(obj);
    else if(type == modalType.EDIT) this.editFunc(obj);
  }
  /**
   * @description: 删除记录-软
   * @param {object} record:需要删除的记录
   */  
  public delBusinessCardUser = (record: tableItem) => {
    deleteBusinessCardUser(record.id).then(() => {
      message.success("删除成功");
      this.getData();
    })
  }
  /**
   * @description: 获取公司信息
   */  
  public getCompanyAddListAPi = () => {
    companyAddListAPi(user.userinfo?.unitId as number).then((res) => {
      console.log(res);
      const data = res.data.data;
      if(data?.addr) this._addrList.value.push(data.addr);
      if(data?.addr2) this._addrList.value.push(data.addr2);
      
      if(data?.name) this._companyList.value.push(data.name);
      if(data?.desName) this._companyList.value.push(data.desName);
      if(data?.desName1) this._companyList.value.push(data.desName1);
      if(data?.desName3) this._companyList.value.push(data.desName3);
    })
  }
}

export default NoteManage;